package cn.htsyb.synergy.security.web.captcha;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/22
 * @since 1.0
 */
public class CaptchaBuilder {
    /**
     * 验证码范围,去掉数字0、1和拼音O、I容易混淆的
     */
    private final char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};
    /**
     * 图片的宽度
     */
    private int width = 120;
    /**
     * 图片的高度
     */
    private int height = 36;
    /**
     * 验证码字符个数
     */
    private int codeCount = 5;
    /**
     * 验证码干扰线数
     */
    private int lineCount = 80;
    /**
     * 验证码
     */
    private String code = null;
    /**
     * 验证码图片Buffer
     */
    private BufferedImage buffImg = null;

    /**
     * 默认构造函数,设置默认参数
     */
    public CaptchaBuilder() {
        //this.makerCaptcha();
    }

    /**
     * @param width  图片宽
     * @param height 图片高
     */
    public CaptchaBuilder(int width, int height) {
        this.width = width;
        this.height = height;
        //this.makerCaptcha();
    }

    /**
     * @param width     图片宽
     * @param height    图片高
     * @param codeCount 字符个数
     * @param lineCount 干扰线条数
     */
    public CaptchaBuilder(int width, int height, int codeCount, int lineCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        //this.makerCaptcha();
    }


    public void makerCaptcha() {
        int drawX = 0, fontHeight = 0, drawY = 0;
        int red = 0, green = 0, blue = 0;
        //每个字符的宽度(左右各空出一个字符)
        drawX = width / (codeCount + 2);
        //字体的高度
        fontHeight = height - 2;
        drawY = height - 4;

        // 图像buffer
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D gd = buffImg.createGraphics();
        Random random = new Random();

        // 填充矩形
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);

        // 设置字体
        Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
        gd.setFont(font);

        //绘制边框
        gd.setColor(Color.green);
        gd.drawRect(0, 0, width, height);

        //绘制随机干扰背景
        for (int i = 0; i < lineCount; i++) {
            // 设置随机开始和结束坐标
            //x坐标开始
            int xs = random.nextInt(width);
            //y坐标开始
            int ys = random.nextInt(height);
            //x坐标结束
            int xe = xs + random.nextInt(width / 8);
            //y坐标结束
            int ye = ys + random.nextInt(height / 8);

            // 产生随机的(140~240)颜色值(值越大颜色越浅)，让输出的每个干扰线的颜色值都将不同。
            red = random.nextInt(60) + 140;
            //控制主题色偏绿色
            green = random.nextInt(60) + 180;
            blue = random.nextInt(60) + 140;

            gd.setColor(new Color(red, green, blue));

            //画线 最多设置150
            gd.drawLine(xs, ys, xe, ye);
            //画圆  最多设置40
            gd.drawOval(xs, ys, xe, ye);

        }

        // 记录验证码
        StringBuffer randomCode = new StringBuffer();
        // 绘制随机验证码。
        for (int i = 0; i < codeCount; i++) {
            //随机获取字符
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            // 产生随机的(40~160)颜色值(值越小颜色越深)，让输出的每个字符的颜色值都将不同。
            //字符与干扰线颜色重叠区间越高，识别难度越大
            red = random.nextInt(80) + 40;
            //控制主题色偏绿色
            green = random.nextInt(80) + 80;
            blue = random.nextInt(80) + 40;

            gd.setColor(new Color(red, green, blue));

            //随机旋转字符角度
            AffineTransform affineTransform = new AffineTransform();
            //随机(-20~20)度倾斜,部分字符重叠
            int randomRadian = (random.nextInt(40) - 20);
            affineTransform.rotate(Math.toRadians(randomRadian), 0, 0);

            Font rotatedFont = font.deriveFont(affineTransform);
            gd.setFont(rotatedFont);

            //绘制字符
            gd.drawString(strRand, (i + 1) * drawX, drawY);
            // 将产生的随机数组合在一起。
            randomCode.append(strRand);

        }
        // 记录字符
        code = randomCode.toString();
    }

    public void writeImageToFile(String path) throws IOException {
        OutputStream sos = new FileOutputStream(path);
        this.writeImageToStream(sos);
    }

    public void writeImageToStream(OutputStream sos) throws IOException {
        ImageIO.write(buffImg, "png", sos);
        sos.close();
    }

    public BufferedImage getBuffImg() {
        return buffImg;
    }

    public String getCode() {
        return code;
    }
}
